<?php
/**
 * *********************************************************************************************************************
 * 身份证识别soa
 * *********************************************************************************************************************
 */
namespace soa\certificates;
use soa\client;

class IDCard extends client
{
    /**
     * 服务地址
     * @var string
     */
    protected $service_url = 'http://core.kuaidihelp.com/';
    /**
     * 服务名称
     * @var string
     */
    protected $service_name = 'kuaidihelp_core';

    /**
     * @param $filename
     * @return string
     */
    public function getName($filename)
    {
        return $this->post('v1/IDCard/getName', array('filename' => $filename));
    }

    /**
     * @param $filename
     * @return string
     */
    public function getSex($filename)
    {
        return $this->post('v1/IDCard/getSex', array('filename' => $filename));
    }

    /**
     * @param $filename
     * @return string
     */
    public function getBorn($filename)
    {
        return $this->post('v1/IDCard/getBorn', array('filename' => $filename));
    }

    /**
     * @param $filename
     * @return string
     */
    public function getAddress($filename)
    {
        return $this->post('v1/IDCard/getAddress', array('filename' => $filename));
    }

    /**
     * @param $filename
     * @return string
     */
    public function getNo($filename)
    {
        return $this->post('v1/IDCard/getNo', array('filename' => $filename));
    }

    /**
     * @param $filename
     * @return string
     */
    public function getNation($filename)
    {
        return $this->post('v1/IDCard/getNation', array('filename' => $filename));
    }

    /**
     * @param $filename
     * @return string
     */
    public function getPortrait($filename)
    {
        return $this->post('v1/IDCard/getPortrait', array('filename' => $filename));
    }

    /**
     * @param $filename
     * @return array
     */
    public function getAllInfo($filename)
    {
        return $this->post('v1/IDCard/getAllInfo', array('filename' => $filename));
    }

    /**
     * 身份证采集的信息保存
     * @param array $dataSave
     * @return bool
     */
    public function save(array $dataSave)
    {
        if(empty($dataSave)) {
            return false;
        }
        //需要检测的字段
        $checkField = array('name', 'idno');
        foreach ($dataSave as $field => $value) {
            if(empty($value) && in_array($field, $checkField)) {
                return false;
            }
        }
        $data = [
            'name' => $dataSave['name'],
            'sex' => $dataSave['sex'] ?: '',
            'nation' => $dataSave['nation'] ?: '',
            'born' => $dataSave['born'] ?: '',
            'address' => $dataSave['address'] ?: '',
            'idno' => $dataSave['idno'],
            'user_id' => $dataSave['user_id'],
            'weixin_openid' => $dataSave['weixin_openid'] ?: '',
            'alipay_openid' => $dataSave['alipay_openid'] ?: '',
            'source' => $dataSave['source'] ?: '',
            'profile' => $dataSave['profile'] ?: '',
            'portrait' => $dataSave['portrait'] ?: '',
            'mobile' => $dataSave['mobile'],
            'status' => $dataSave['status'] ?: 0
        ];
        $result = $this->post('v1/IDCard/save', $data);

        if($result == 1) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 实名认证资料查询
     * @param $user_id
     * @param $weixin_openid
     * @param $alipay_openid
     * @param $idno
     * @return array|bool
     */
    public function getIdentityInfo($user_id, $weixin_openid = '', $alipay_openid = '', $idno = '')
    {
        if(empty($user_id) && empty($weixin_openid) && empty($alipay_openid) && empty($idno)) {
            return array();
        }

        $data = [
            'user_id' => $user_id,
            'weixin_openid' => $weixin_openid,
            'alipay_openid' => $alipay_openid,
            'idno' => $idno
        ];

        $data = array_filter($data);

        return $this->get('v1/IDCard/getIdentityInfo', $data);

    }

    public function getOrderIdentityInfo($order_no)
    {
        if(empty($order_no)){
            return array();
        }

        return $this->get(
            'v1/enrollment/getInfo',
            array('order_no' => $order_no)
        );
    }
}